home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / lptalk-1.3 / util.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  6KB  |  336 lines

  1. /************************************************************************/
  2. /* LP-Talk
  3.     Version 1.0 [ 9/24/90]
  4.     Version 1.1 [ 9/27/90]
  5.     Version 1.2 [ 9/28/90]
  6. */
  7. /* TinyTalk utilities.                            */
  8. /*                                    */
  9. /*    Version 1.0 [ 1/24/90] : Initial implementation by ABR.        */
  10. /*        1.1 [ 1/25/90] : Moved terminal control here.        */
  11. /*        1.2 [ 1/25/90] : Modified to set termcap var 'ospeed'.    */
  12. /*        1.3 [ 2/ 2/90] : Added comparestr() and hiliting.    */
  13. /*        1.4 [ 2/ 5/90] : Added expand_filename() for $#@! UNIX. */
  14. /*                 Disable its fancy stuff because there  */
  15. /*                 are no shared libraries.        */
  16. /*        1.5 [ 2/16/90] : Integrated support for System V and    */
  17. /*                 HP-UX, from Andy Norman and Kipp       */
  18. /*                 Hickman.                */
  19. /*                                    */
  20. /************************************************************************/
  21.  
  22. #include "tl.h"
  23.  
  24. #ifdef HPUX
  25.  
  26. #include <sys/ioctl.h>
  27. #include <termio.h>
  28.  
  29. #else /* not HPUX */
  30. #ifdef SYSV
  31.  
  32. #include <termio.h>
  33. static int first_time = 1;
  34. static struct termio old_tty_state;
  35.  
  36. #else /* BSD */
  37.  
  38. #include <sys/ioctl.h>
  39. #include <sgtty.h>
  40.  
  41. #endif /* SYSV */
  42. #endif /* HPUX */
  43.  
  44. #include <pwd.h>
  45. #include <stdio.h>
  46.  
  47. #undef FANCY
  48.  
  49. extern char *getenv(), *index();
  50. extern struct passwd *getpwnam();
  51.  
  52. #ifdef TERMCAP
  53. extern short ospeed;
  54. static int have_hilite;
  55. static char hilite_on_string[16], hilite_off_string[16];
  56. static char write_buffer[64];
  57. static char *write_buffer_ptr;
  58. #endif
  59.  
  60. uppercase(s, t)
  61.   register char *s, *t;
  62. {
  63.   while (*s) {
  64.     *t = (((*s >= 'a') && (*s <= 'z')) ? *s-32 : *s);
  65.     s++;
  66.     t++;
  67.   }
  68.   *t = '\0';
  69. }
  70.  
  71. int equalstr(s, t)
  72.   char *s, *t;
  73. {
  74.   string su, tu;
  75.  
  76.   uppercase(s,su);
  77.   uppercase(t,tu);
  78.   return (!strcmp(su,tu));
  79. }
  80.  
  81. int comparestr(s, t)
  82.   char *s, *t;
  83. {
  84.   string su, tu;
  85.  
  86.   uppercase(s,su);
  87.   uppercase(t,tu);
  88.   return (strcmp(su,tu));
  89. }
  90.  
  91. stripstr(s)                /* Strip leading & trailing */
  92.   register char *s;            /* spaces from a string. */
  93. {
  94.   string temp;
  95.   register char *from;
  96.  
  97.   if (s[0] == ' ') {            /* Remove leading spaces. */
  98.     from = s + 1;
  99.     while (*from == ' ')
  100.       from++;
  101.     strcpy(temp, from);
  102.     strcpy(s, temp);
  103.   }
  104.  
  105.   from = s + strlen(s) - 1;
  106.   if (*from == ' ') {
  107.     --from;
  108.     while (*from == ' ')
  109.       --from;
  110.     *(from+1) = '\0';
  111.   }
  112. }
  113.  
  114. #ifdef HPUX
  115.  
  116. cbreak_noecho_mode()
  117. {
  118.   struct termio blob;
  119.  
  120.   if (ioctl(0, TCGETA, &blob) == -1)
  121.     perror("TCGETA ioctl");
  122.  
  123.   blob.c_lflag &= ~ECHO;
  124.   blob.c_lflag &= ~ECHOE;
  125.   blob.c_lflag &= ~ICANON;
  126.   blob.c_cc[VMIN] = 0;
  127.   blob.c_cc[VTIME] = 0;
  128.  
  129.   if (ioctl(0, TCSETAF, &blob) == -1)
  130.     perror("TCSETAF ioctl");
  131.  
  132. }
  133.  
  134. cooked_echo_mode ()
  135. {
  136.   struct termio blob;
  137.  
  138.   if (ioctl(0, TCGETA, &blob) == -1)
  139.     perror("TCGETA ioctl");
  140.   
  141.   blob.c_lflag |= ECHO;
  142.   blob.c_lflag |= ECHOE;
  143.   blob.c_lflag |= ICANON;
  144.  
  145.   if (ioctl(0, TCSETAF, &blob) == -1)
  146.     perror("TCSETAF ioctl");
  147.   
  148. }
  149.  
  150. #else /* not HPUX */
  151.  
  152. #ifdef SYSV
  153.  
  154. cbreak_noecho_mode()            /* When we're running... */
  155. {
  156.   struct termio blob;
  157.  
  158.   if (first_time) {
  159.     ioctl(0, TCGETA, &old_tty_state);
  160.     first_time = 0;
  161.   }
  162.  
  163.   ioctl(0, TCGETA, &blob);
  164.   blob.c_cc[VMIN] = 0;
  165.   blob.c_cc[VTIME] = 0;
  166.   blob.c_iflag = IGNBRK | IGNPAR | ICRNL;
  167.   blob.c_oflag = OPOST | ONLCR;
  168.   blob.c_lflag = ISIG;
  169.   ioctl(0, TCSETA, &blob);
  170. }
  171.  
  172. cooked_echo_mode()            /* When we exit... */
  173. {
  174.   if (!first_time) {
  175.     ioctl(0, TCSETA, &old_tty_state);
  176.   }
  177. }
  178.  
  179. #else /* BSD */
  180.  
  181. cbreak_noecho_mode()            /* When we're running... */
  182. {
  183.   struct sgttyb blob;
  184.  
  185.   if (ioctl(0, TIOCGETP, &blob) == -1)
  186.     perror("TIOCGETP ioctl");
  187.   blob.sg_flags |= CBREAK;
  188.   blob.sg_flags &= ~ECHO;
  189.   if (ioctl(0, TIOCSETP, &blob) == -1)
  190.     perror("TIOCSETP ioctl");
  191.  
  192. #ifdef TERMCAP
  193.   ospeed = blob.sg_ospeed;
  194. #endif /* TERMCAP */
  195. }
  196.  
  197. cooked_echo_mode()            /* When we exit... */
  198. {
  199.   struct sgttyb blob;
  200.  
  201.   if (ioctl(0, TIOCGETP, &blob) == -1)
  202.     perror("TIOCGETP ioctl");
  203.   blob.sg_flags &= ~CBREAK;
  204.   blob.sg_flags |= ECHO;
  205.   if (ioctl(0, TIOCSETP, &blob) == -1)
  206.     perror("TIOCSETP ioctl");
  207. }
  208.  
  209. #endif /* SYSV */
  210. #endif /* HPUX */
  211.  
  212. die(why, s1, s2, s3, s4)
  213.   char *why, *s1, *s2, *s3, *s4;
  214. {
  215.   cooked_echo_mode();            /* Reset terminal characteristics. */
  216.   disable_logging();            /* Flush and close log file. */
  217.   fprintf(stderr, why, s1, s2, s3, s4);
  218.   exit(1);
  219. }
  220.  
  221. init_hilite_utils()
  222. {
  223. #ifdef TERMCAP
  224.  
  225.   char blob[1024];            /* As per termcap man page */
  226.   char *terminal_name, *temp;
  227.  
  228.   have_hilite = FALSE;
  229.  
  230.   terminal_name = getenv("TERM");
  231.   if (terminal_name == NULL)
  232.     return;
  233.  
  234.   if (tgetent(blob, terminal_name) != 1)
  235.     return;
  236.  
  237.   temp = hilite_on_string;
  238.  
  239.   if (tgetstr("md",&temp) != NULL) {
  240.     temp = hilite_off_string;
  241.     if (tgetstr("me",&temp) != NULL)
  242.       have_hilite = TRUE;
  243.   }
  244.  
  245.   if (!have_hilite) {
  246.     temp = hilite_on_string;
  247.  
  248.     if (tgetstr("so",&temp) != NULL) {
  249.       temp = hilite_off_string;
  250.       if (tgetstr("se",&temp) != NULL)
  251.     have_hilite = TRUE;
  252.     }
  253.   }
  254.  
  255. #endif
  256. }
  257.  
  258. #ifdef TERMCAP
  259. output_one(c)
  260.   char c;
  261. {
  262.   *(write_buffer_ptr++) = c;
  263. }
  264. #endif
  265.  
  266. hilite_on()
  267. {
  268. #ifdef TERMCAP
  269.  
  270.   if (have_hilite) {
  271.     write_buffer_ptr = write_buffer;
  272.     tputs(hilite_on_string, 1, output_one);
  273.     write(1, write_buffer, write_buffer_ptr - write_buffer);
  274.   }
  275.  
  276. #endif
  277. }
  278.  
  279. hilite_off()
  280. {
  281. #ifdef TERMCAP
  282.  
  283.   if (have_hilite) {
  284.     write_buffer_ptr = write_buffer;
  285.     tputs(hilite_off_string, 1, output_one);
  286.     write(1, write_buffer, write_buffer_ptr - write_buffer);
  287.   }
  288.  
  289. #endif
  290. }
  291.  
  292. expand_filename(s)
  293.   string s;
  294. {
  295.   char *env;
  296.   string temp;
  297.  
  298.   if (s[0] != '~')            /* Only affect stuff with tildes. */
  299.     return;
  300.  
  301.   if (s[1] == '/') {            /* Current user's home directory. */
  302.     env = getenv("HOME");
  303.     if (env == NULL)
  304.       temp[0] = '\0';
  305.     else
  306.       strcpy(temp, env);
  307.  
  308.     strcat(temp, s+1);            /* {home}/whatever */
  309.     strcpy(s, temp);
  310.   }
  311.  
  312. #ifdef FANCY
  313.  
  314.   else if (s[1] != '\0') {        /* Someone else's home directory. */
  315.     struct passwd *tt;
  316.     char *loc;
  317.  
  318.     loc = index(s,'/');
  319.     if (loc != NULL) {            /* No slash ==> we don't care. */
  320.       *loc = '\0';            /* Get rid of slash. */
  321.       setpwent();
  322.       tt = getpwnam(s+1);        /* Ignore leading tilde. */
  323.       endpwent();
  324.       *loc = '/';            /* Restore slash. */
  325.       if (tt != NULL) {            /* Can't find them ==> don't care. */
  326.     strcpy(temp, tt->pw_dir);
  327.     strcat(temp, loc);        /* {their home}/whatever */
  328.     strcpy(s, temp);
  329.       }
  330.     }
  331.   }
  332.  
  333. #endif /* FANCY */
  334.  
  335. }
  336.